﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using JumbotCms.API;
namespace JumbotCms.Modules.Journal.Plus
{
    public partial class _down : JumbotCms.API.FrontHtml
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Server.ScriptTimeout = 8;//脚本过期时间
            if (!CheckCookiesCode())
            {
                Response.Write("验证码错误");
                Response.End();
            }
            string id = Str2Str(q("id"));
            string ChannelId = Str2Str(q("ChannelId"));
            if (id == "0")
            {
                FinalMessage("请不要修改地址栏参数!", site.Dir, 0, 8);
                Response.End();
            }
            //判断是杂志地址还是原文地址
            int _NO = Jumbot.Common.Validator.StrToInt(q("NO"), 1);
            //如果不是1也不是2，那就是自己改参数了
            if (_NO != 1 && _NO != 2)
            {
                FinalMessage("请不要修改地址栏参数!", site.Dir, 0, 8);
                Response.End();
            }
            doh.Reset();
            doh.ConditionExpress = "ChannelId=" + ChannelId + " and id=" + id;
            object[] _obj = doh.GetValues("jcms_module_journal", "DownUrl" + _NO + ",Points,Title");
            string downUrl = _obj[0].ToString();
            int _Points = Str2Int(_obj[1].ToString(), 0);
            string _JournalTitle = _obj[2].ToString();
            //如果地址不为空
            if (downUrl != "")
            {
                if (_NO == 1)
                {
                    if (!CanDownFile(ChannelId, id, _Points, _JournalTitle))
                    {
                        Response.Redirect("~/errordown.aspx");
                        Response.End();
                    }
                    else
                        DownloadFile(downUrl);
                }
                else
                    DownloadFile(downUrl);
            }
            else
                FinalMessage("当前下载地址为空!", site.Dir, 0, 8);
        }
        /// <summary>
        /// 判断附件下载权限
        /// 并扣除相应的点数
        /// </summary>
        /// <param name="_ChannelId">频道ID</param>
        /// <param name="_JournalId">内容ID</param>
        /// <param name="_Points">扣除点数</param>
        /// <param name="_JournalTitle">杂志期号</param>
        /// <returns></returns>
        private bool CanDownFile(string _ChannelId, string _JournalId, int _Points, string _JournalTitle)
        {
            string _UserId = "0";
            string _UserName = "";
            if (Jumbot.Common.Cookie.GetValue(site.CookiePrev + "user") != null)
            {
                if (_Points == 0)
                    return true;
                _UserId = Jumbot.Common.Cookie.GetValue(site.CookiePrev + "user", "id");
                _UserName = Jumbot.Common.Cookie.GetValue(site.CookiePrev + "user", "name");
                JumbotCms.DataObject.User _User = new JumbotCms.DataObject.User();
                if (!_User.VIP(_UserId, doh))//给会员扣除点数,VIP不扣除
                {
                    doh.Reset();
                    //为什么要弄一个DownDegree<50？一个会员连续下载一个文件50次是很可疑的
                    //不是刷分就是恶意下载，做个统计留意一下
                    doh.ConditionExpress = "DownDegree<50 and ChannelId=" + _ChannelId + " and JournalId=" + _JournalId + " and UserId=" + _UserId;
                    if (doh.Exist("jcms_module_journal_downlogs"))//说明已经扣过
                    {
                        doh.Reset();
                        doh.ConditionExpress = "DownDegree<50 and ChannelId=" + _ChannelId + " and JournalId=" + _JournalId + " and UserId=" + _UserId;
                        doh.Add("jcms_module_journal_downlogs", "DownDegree");
                        return true;
                    }
                    doh.Reset();
                    doh.ConditionExpress = "id=" + _UserId;
                    int _myPoints = Str2Int(doh.GetValue("jcms_normal_user", "Points").ToString());
                    if (_myPoints < _Points)//说明点数不够
                        return false;
                    doh.Reset();
                    doh.ConditionExpress = "id=" + _UserId;
                    doh.Substract("jcms_normal_user", "Points", _Points);
                    string _OperInfo1 = "下载杂志:<a href=\"" + Go2View(doh, 1, false, _ChannelId, _JournalId) + "\" target=\"_blank\">" + _JournalTitle + "</a>，扣除了" + _Points + "个点";
                    SaveUserLog(_UserId, _OperInfo1, 2);
                    //增加一个下载日志记录
                    doh.Reset();
                    doh.AddFieldItem("UserId", _UserId);
                    doh.AddFieldItem("ChannelId", _ChannelId);
                    doh.AddFieldItem("JournalId", _JournalId);
                    doh.AddFieldItem("Points", _Points);
                    doh.AddFieldItem("DownTime", DateTime.Now.ToString());
                    doh.AddFieldItem("DownIP", GetUserIp());
                    doh.AddFieldItem("DownNO", 1);
                    doh.AddFieldItem("DownDegree", 1);
                    doh.Insert("jcms_module_journal_downlogs");
                }
                return true;
            }
            else
                return false;
        }
    }
}
